找传奇、传世资源到传世资源站!

C# 录制语音 保存成mp3格式 并播放

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 录制语音 保存成mp3格式 并播放 C#语言基础-第1张 public partial class AudioMsgForm : Form { private int fileIndex; private AudioFileMaker audioFileMaker = new AudioFileMaker(); private IAudioPlayer audioPlayer; private IMicrophoneCapturer microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0); private Byte[] audioData = new byte[0]; public string StorePath { get { return String.Format("语音备忘\\AudioMsg{0}.ad", this.fileIndex); } } public AudioMsgForm() { InitializeComponent(); this.microphoneCapturer.AudioCaptured = new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured); this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, this.microphoneCapturer.SampleRate, this.microphoneCapturer.ChannelCount); if(!Directory.Exists("语音备忘")) { Directory.CreateDirectory("语音备忘"); } string[] fileName = Directory.GetFiles("语音备忘"); foreach (string name in fileName) { this.fileIndex ; } } void microphoneCapturer_AudioCaptured(byte[] data) { this.audioData = BufferJointer.Joint(this.audioData, data); this.decibelDisplayer1.DisplayAudioData(data); } private void button_start_Click(object sender, EventArgs e) { this.microphoneCapturer.Start(); this.label_notify.Text = "正在录音!"; this.label_notify.ForeColor = Color.Red; this.button_start.Enabled = false; this.button_stop.Enabled = true; } private void button_stop_Click(object sender, EventArgs e) { this.microphoneCapturer.Stop(); File.WriteAllBytes(this.StorePath, this.audioData); this.label_notify.Text = "录音完成!"; this.label_notify.ForeColor = Color.Blue; this.LoadAduioFileList(); this.button_start.Enabled = true; this.button_stop.Enabled = false; this.fileIndex ; return; } private void toolStripButton1_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.toolStripComboBox1.SelectedText)) { MessageBox.Show("请选择列表中的一项!"); this.toolStripComboBox1.DroppedDown = true; return; } this.audioPlayer.Play(File.ReadAllBytes("语音备忘\\" this.toolStripComboBox1.SelectedText)); } private void AudioMsgForm_Shown(object sender, EventArgs e) { this.LoadAduioFileList(); this.button_stop.Enabled = false; } private void LoadAduioFileList() { this.toolStripComboBox1.Items.Clear(); string[] fileName = Directory.GetFiles("语音备忘"); foreach (string name in fileName) { string shortName = name.Substring(name.LastIndexOf("\\") 1, name.Length - 1 - name.LastIndexOf("\\")); this.toolStripComboBox1.Items.Add(shortName); } } private void toolStripButton2_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.toolStripComboBox1.SelectedText)) { MessageBox.Show("请选择列表中的一项!"); this.toolStripComboBox1.DroppedDown = true; return; } if (!Directory.Exists("mp3目录")) { Directory.CreateDirectory("mp3目录"); } string path= String.Format("mp3目录\\{0}.mp3", Path.GetFileNameWithoutExtension(this.toolStripComboBox1.SelectedText)); this.audioFileMaker.Initialize(path, this.microphoneCapturer.SampleRate, this.microphoneCapturer.ChannelCount); this.audioFileMaker.AddAudioFrame(File.ReadAllBytes("语音备忘\\" this.toolStripComboBox1.SelectedText)); this.audioFileMaker.Close(true); DialogResult dialogResult = MessageBox.Show("转存成功,是否打开目录查看?", "提醒", MessageBoxButtons.YesNo); if (dialogResult == System.Windows.Forms.DialogResult.Yes) { Process.Start("explorer.exe", "mp3目录"); } } }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复